package com.dindang.propertyfee.app.utils;

import com.dindang.propertyfee.mvp.model.entity.Build;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxw on & 2019/1/15
 */
public class NodesTreeUtils {
    public static void main(String[] args) {
        //一维
        List<Build.Nodes> list = getChilds(data, 1);
        for(Build.Nodes l : list){
            System.out.println(l.text);
        }
        System.out.println("===============");
        //多维
        List<Build.Nodes> list2 = getChildsManyGroup(data, 1);
        for(Build.Nodes l : list2){
            System.out.println(l.text);
            for(Build.Nodes ll : l.getList()){
                System.out.println("\t"+ll.text);
                for(Build.Nodes lll : ll.getList()){
                    System.out.println("\t\t"+lll.text);
                    for(Build.Nodes lllL : lll.getList()){
                        System.out.println("\t\t\t"+lllL.text);

                    }
                }
            }
        }
    }
    //查询数据库表中所有数据,(这里模拟一个表的所有数据)
    static List<Build.Nodes> data = new ArrayList<>();
    /**
     * 根据id 获取所有父级目录 一维
     * 用途（首页 > 编程语言 > JAVA >  hibernate > hibernate入门教程）类似这样的一个系统当前位置   传入位置获取到所有父级
     * @param list
     * @param pid
     * @return
     */
    public static List<Build.Nodes> getParents(List<Build.Nodes> list,int pid){
        List<Build.Nodes> arr = new ArrayList<Build.Nodes>();
        for (Build.Nodes location : list) {
            if(pid == location.mId){
                arr.addAll(getParents(list, location.mPid));
                arr.add(location);  break;
            }
        }
        return arr;
    }
    /**
     * 根据id获取所有子集分类(一维List集合)
     * 1 11 111
     * @param list
     * @param pid
     * @return
     */
    public static List<Build.Nodes> getChilds(List<Build.Nodes> list,int pid){
        List<Build.Nodes> arr = new ArrayList<Build.Nodes>();
        for(Build.Nodes location : list){
            if(pid == location.mPid){
                arr.addAll(getChilds(list, location.mId));
                arr.add(location);

            }
        }
        return arr;
    }
    /**
     * 根据id返回所有子集分类,(多维List集合,List中含有子集List)
     *
     *  1
     *    11
     *       111
     *  2
     *    22
     *       222
     * @param list
     * @param pid
     * @return
     */
    public static List<Build.Nodes> getChildsManyGroup(List<Build.Nodes> list,int pid){
        List<Build.Nodes> arr = new ArrayList<Build.Nodes>();
        for(Build.Nodes location : list){
            if(pid == location.mPid){
                location.setList(getChildsManyGroup(list, location.mId));
                arr.add(location);
            }
        }
        return arr;
    }

    public static String getChildsId(List<Build.Nodes> list){
        StringBuffer s = new StringBuffer();
        for (Build.Nodes location : list) {
            s.append(location.text);
            if(location.list.size() > 0){
                s.append("/");
                s.append(getChildsId(location.list));
            }else{
                s.append("^");
            }
        }

        String nodeValue = s.toString();
        if(nodeValue.length() > 0){
            nodeValue.substring(0, nodeValue.length()-1);
        }

        return nodeValue;
    }

    /**
     * 组合为一维集合
     * @param list
     * @param pid
     * @return
     */
    public static List<Build.Nodes> pushOneGroup(List<Build.Nodes> list,int pid){
        List<Build.Nodes> arr = new ArrayList<Build.Nodes>();
        for (Build.Nodes location : list) {
            if(pid == location.mPid){
                arr.add(location);
                arr.addAll(pushOneGroup(list, location.mId));
            }
        }
        return arr;
    }
    /**
     * 组合为多维集合
     * 用途：系统首页的导航菜单，常见的2-3级通过下面的方法压栈成多维集合在供前台显示
     * @param list 要便利的集合数据
     * @param pid  父id
     * @return
     */
    public static List<Build.Nodes> pushManyGroup(List<Build.Nodes> list,int pid){
        List<Build.Nodes> arr = new ArrayList<Build.Nodes>();
        for (Build.Nodes location : list) {
            if(pid == location.mPid){
                location.setList(pushManyGroup(list, location.mId));
                arr.add(location);
            }
        }
        return arr;
    }
}
